package ee.android.kinokavad.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import ee.android.kinokavad.R;
import ee.android.kinokavad.data.Cinema;

public class CinemasAdapter extends BaseExpandableListAdapter implements OnClickListener{

	private static final int [] ids = new int [] {R.id.cinemaRowDirections, R.id.cinemaRowHomePage, R.id.cinemaRowEmail, R.id.cinemaRowPhone};
	private static final String TAG = CinemasAdapter.class.getSimpleName();
	private List<Cinema> cinemas;
	private LayoutInflater inflater; 
	private Context context;

	public CinemasAdapter(Context context, List<Cinema> cinemas) {
		this.cinemas = cinemas;
		this.inflater = LayoutInflater.from(context);
		this.context = context;
	}

	private static class ChildHolder {
		private TextView location;
		private TextView info;
		private ImageView logo;
	}

	@Override
	public void onClick(View v) {

		Cinema cinema = cinemas.get((Integer) v.getTag());

		switch (v.getId()) {
		case R.id.cinemaRowDirections:
			handleDirectionsClick(cinema.getLat(), cinema.getLon(), cinema.getAddress(), cinema.getName());
			break;
		case R.id.cinemaRowHomePage:
			handleHomePageClick(cinema.getHomePage());
			break;
		case R.id.cinemaRowEmail:
			handleEmailClick(cinema.getEmail());
			break;
		case R.id.cinemaRowPhone:
			handlePhoneClick(cinema.getPhone());
			break;

		default:
			break;
		}

	}

	private void handlePhoneClick(String number) {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:"+number));
		context.startActivity(intent); 
	}
	private void handleEmailClick(String email) {
		Intent i = new Intent(Intent.ACTION_SEND);
		i.setType("message/rfc822");
		i.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
		i.putExtra(Intent.EXTRA_SUBJECT, "");
		i.putExtra(Intent.EXTRA_TEXT, "");
		try {
			context.startActivity(Intent.createChooser(i, "Saada email..."));
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(context, "Ühtegi emaili klienti pole installitud.", Toast.LENGTH_SHORT).show();
		}
	}
	private void handleHomePageClick(String url) {
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
		context.startActivity(browserIntent);
	}
	private void handleDirectionsClick(double latitude, double longitude, String address, String name) {
		if (name.equals("Mai")) name = "Mai Kino";
		else if (name.equals("Kosmos") || name.equals("Ekraan")) name = "";
		String uri = String.format("geo:%f,%f?q=%s %s", latitude, longitude, address, name);
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
		context.startActivity(intent);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return cinemas.get(groupPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		ChildHolder holder = null;
		if (convertView == null) {
			holder = new ChildHolder();
			convertView = inflater.inflate(R.layout.cinema_row, null);
			holder.info = (TextView) convertView.findViewById(R.id.cinemaRowInfo);
			holder.logo = (ImageView) convertView.findViewById(R.id.cinemaRowLogo);
			holder.location = (TextView) convertView.findViewById(R.id.cinemaRowLocation);
			convertView.setTag(holder);
		} else {
			holder = (ChildHolder) convertView.getTag();
		}

		Cinema cinema = cinemas.get(groupPosition);
		holder.info.setText(cinema.getInfo());
		holder.logo.setImageResource(cinema.getIcon());
		holder.location.setText(cinema.getAddress());
		
		for (int id : ids) {
			View view = convertView.findViewById(id);
			view.setTag(groupPosition);
			view.setOnClickListener(this);
		}

		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return cinemas.get(groupPosition).getName();
	}

	@Override
	public int getGroupCount() {
		return cinemas.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	private class GroupHolder {
		private CheckBox selectedBox;
		private TextView name;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		GroupHolder holder = null;
		if (convertView == null) {
			holder = new GroupHolder();
			convertView = inflater.inflate(R.layout.cinema_group_row, null);
			holder.name = (TextView) convertView.findViewById(R.id.cinemaGroupRowName);
			holder.selectedBox = (CheckBox) convertView.findViewById(R.id.cinemaGroupRowCheckBox);
			holder.selectedBox.setOnClickListener( new View.OnClickListener() {  
				public void onClick(View v) {  
					CheckBox cb = (CheckBox) v ;  
					Cinema cinema = (Cinema) cb.getTag();  
					cinema.setSelected(cb.isChecked());  
				}  
			}); 
			convertView.setTag(holder);
		} else {
			holder = (GroupHolder) convertView.getTag();
		}
		Cinema cinema = cinemas.get(groupPosition);
		holder.name.setText(cinema.getName().toUpperCase());
		holder.selectedBox.setTag(cinema);
		holder.selectedBox.setChecked(cinema.isSelected());

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
